package org.yoki.tool.core;

import org.yoki.tool.core.bean.BeanTemplateBean;
import org.yoki.tool.core.bean.TableTemplateBean;
import org.yoki.tool.core.build.BasicBuilder;
import org.yoki.tool.core.config.BuilderConfig;
import org.yoki.tool.core.helper.JDBCConnectHelper;
import org.yoki.tool.core.helper.TableBuildHelper;
import org.yoki.tool.core.helper.TemplateChangeHelper;

import java.util.List;

/**
 * Created by SunnyWen on 2017/6/24.
 */
public class CodeBuildFactory {

    private BuilderConfig builderConfig;

    private BasicBuilder beanBuilder;

    public CodeBuildFactory(){
    }

    public CodeBuildFactory(BuilderConfig builderConfig, BasicBuilder beanBuilder){
        this.builderConfig = builderConfig;
        this.beanBuilder = beanBuilder;
    }

    public void build() {
        JDBCConnectHelper jdbcConnectHelper = new JDBCConnectHelper(this.builderConfig.getDatabaseConfig()) ;
        TableBuildHelper tableBuildHelper = new TableBuildHelper(jdbcConnectHelper.getConnection()) ;
        List<TableTemplateBean> list = tableBuildHelper.buildTableList() ;
        TemplateChangeHelper templateChangeHelper = new TemplateChangeHelper(builderConfig) ;
        List<BeanTemplateBean> beanList = templateChangeHelper.changeTableList(list) ;
        if(null == beanBuilder.getBeanTemplateBeanList() || beanBuilder.getBeanTemplateBeanList().size() <= 0)
            beanBuilder.setBeanTemplateBeanList(beanList);
        if(null == beanBuilder.getBuilderConfig())
            beanBuilder.setBuilderConfig(this.builderConfig);
        beanBuilder.build();
    }

    public BuilderConfig getBuilderConfig() {
        return builderConfig;
    }

    public void setBuilderConfig(BuilderConfig builderConfig) {
        this.builderConfig = builderConfig;
    }

    public BasicBuilder getBeanBuilder() {
        return beanBuilder;
    }

    public void setBeanBuilder(BasicBuilder beanBuilder) {
        this.beanBuilder = beanBuilder;
    }
}
